// pages/home/home.js
/* 
TODO:
记得远程数据库需要做修改：
user表增加quesIndex字段 （用户当前答题的位置）

*/
const request = require('../../utils/promise.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    testTxt: "",
    username: "",
  },

  /**
   * 
   */
  binderror(e) {
    // console.log(e)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUserName()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  getUserName() {

    var that = this
    console.log("testReq  ")
    request.GET('/getQAUser/getName', {
      id: "9"
    }).then(res => {
      console.log(res)
      if (res.data.resCode == 200) {
        wx.setStorageSync('userId', wx.getStorageSync('userId'))
        wx.setStorageSync('username', "社员")
        var username = wx.getStorageSync('username')
        that.setData({
          username: username
        })
      } else {
        wx.showToast({
          title: '获取用户信息异常，可能未登录'
        })
      }
    })



  },

  gotoAnswerPage() {
    wx.navigateTo({
      url: '/pages/answerPage/answerPageOld',
    })
  },

  gotoUploadPage() {
    wx.navigateTo({
      url: '/pages/uploadQuestion/uploadQuestion',
    })
  },

  gotoOverPage() {
    wx.navigateTo({
      url: '/pages/qaOver/qaOver',
    })
  },

  reqTopNewsListData() {
    request.GET('/news/pageList', {
      topNum: 10
    }).then(res => {
      // console.log(res)
      if (res.data.resCode == 200) {
        this.setData({
          newsArr: res.data.newsArrList
        })
      } else {
        wx.showToast({
          title: '获取数据异常',
          icon: 'error'
        })
      }
    })
  },
  /**
   * 测试请求
   */
  testReq() {
    console.log("testReq  ")
    request.GET('/getQAUser/getName', {
      id: "9"
    }).then(res => {
      console.log(res)
      if (res.data.resCode == 200) {
        /*   this.setData({
            newsArr: res.data.newsArrList
          }) */
      } else {
        /* wx.showToast({
          title: '获取数据异常',
          icon: 'error'
        }) */
      }
    })
  },



})